package com.cyandpos.pad.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.cyandpos.pad.R;
import com.cyandpos.pad.activity.MainActivity;
import com.cyandpos.pad.activity.PayActivity;
import com.cyandpos.pad.activity.RefundActivity;
import com.cyandpos.pad.cateringformat.CateringMainActivity;
import com.cyandpos.pad.cateringformat.choosemenu.MenuFoodActivity;
import com.cyandpos.pad.common.commondata.Constant;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.commondata.MemberIdentificationMethodConstant;
import com.cyandpos.pad.common.commondata.SktConfigConstant;
import com.cyandpos.pad.common.commondata.SktConfigIdCrmConstant;
import com.cyandpos.pad.posdata.QueryMemberReq;
import com.cyandpos.pad.posdata.QueryMemberRequest;
import com.cyandpos.pad.posdata.Sktconfig;
import com.cyandpos.pad.sale.view.SelfNumKeyView;
import com.cyandpos.pad.utils.log.LogUtils;

/**
 * Created by xin on 2021/5/27
 * desc:
 */
public class MemberUtils {
    public static AlertDialog mDialog;//所有dialog
    /**
     *会员识别方式
     * @param mContext
     * @param flag  1 卡券  2储值卡  3 编码券  4 返券后刷卡   5退货会员识别 6查询会员
     */
    public static void showQueryMemberDialog(final Activity mContext, final int flag) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.dialog_member, null);
        TextView tvName = view.findViewById(R.id.tv_dialog_title_name);
        final SelfNumKeyView sf_containt = view.findViewById(R.id.member_soft_keyboard);

        boolean isNeedInputView = false;//是否需要输入布局

        tvName.setText("选择会员方式");
        //关闭按钮
        tvName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
                closeTypeDialog(mContext,flag);
            }
        });

        view.findViewById(R.id.tv_sale_member_swipe).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_scan).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_card).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_phone).setVisibility(View.GONE);
        view.findViewById(R.id.tv_sale_member_idcard).setVisibility(View.GONE);

        String memberList = handleMemberIdentificationList(flag);
        LogUtils.d("Member","字符串:"+memberList);

        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_SWIPE)){
            view.findViewById(R.id.tv_sale_member_swipe).setVisibility(View.VISIBLE);
        }
        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_SCAN)){
            view.findViewById(R.id.tv_sale_member_scan).setVisibility(View.VISIBLE);
        }
        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_CARDNO)){
            view.findViewById(R.id.tv_sale_member_card).setVisibility(View.VISIBLE);
            isNeedInputView = true;
        }
        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_PHONE_NUMBER)){
            view.findViewById(R.id.tv_sale_member_phone).setVisibility(View.VISIBLE);
            isNeedInputView = true;
        }
        if(memberList.contains(MemberIdentificationMethodConstant.MEMBER_METHOD_IDENTIFICATION_NUMBER)){
            view.findViewById(R.id.tv_sale_member_idcard).setVisibility(View.VISIBLE);
            isNeedInputView = true;
        }
        if(!isNeedInputView)
            sf_containt.setVisibility(View.GONE);//不需要输入布局

        //会员刷卡
        view.findViewById(R.id.tv_sale_member_swipe).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
//                swipeCardtypeDialog(mContext,flag);
            }
        });
        //会员扫码
        view.findViewById(R.id.tv_sale_member_scan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mDialog.dismiss();
//                scanCodeTypeDialog(mContext,flag);
            }
        });

        //卡号
        final EditText etCardNo = view.findViewById(R.id.et_sale_member_card);
        final TextView tvCardNo = view.findViewById(R.id.tv_sale_member_card);

        //手机号
        final EditText etPhone = view.findViewById(R.id.et_sale_member_phone);
        final TextView tvPhone = view.findViewById(R.id.tv_sale_member_phone);

        tvCardNo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                tvCardNo.setVisibility(View.GONE);
                etCardNo.setVisibility(View.VISIBLE);

                tvPhone.setVisibility(View.VISIBLE);
                etPhone.setVisibility(View.GONE);

                etPhone.setText("");
                etCardNo.setFocusable(true);
                sf_containt.setEnterReceptor(etCardNo);
                sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
                    @Override
                    public void keyBoardSure(String num,String hint) {
                        mDialog.dismiss();
                        ToastUtils.show("卡号开始查询会员");
                        //卡号事件
                        cardNoTypeDialog(mContext,flag,num);
                    }
                });
            }
        });
        CommonUtils.setNoSoftware(etCardNo, mContext,true);
        CommonUtils.setNoSoftware(etPhone, mContext,true);

        tvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvPhone.setVisibility(View.GONE);
                etPhone.setVisibility(View.VISIBLE);

                tvCardNo.setVisibility(View.VISIBLE);
                etCardNo.setVisibility(View.GONE);

                etCardNo.setText("");
                etPhone.setFocusable(true);
                sf_containt.setEnterReceptor(etPhone);
                sf_containt.setOnCallBack(new SelfNumKeyView.SelfNumCallBack() {
                    @Override
                    public void keyBoardSure(String num,String hint) {
                        mDialog.dismiss();
                        ToastUtils.show("手机号开始查询会员");
                        //手机号事件
                        phoneTypeDialog(mContext,flag,num);
                    }
                });

            }
        });
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
        dialog.setView(view).setCancelable(false).create();
        mDialog = dialog.show();
    }

    private static String handleMemberIdentificationList(int flag) {
        String memberType="";
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON:
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
            case Constant.MEMBER_IDENTIFYING_REFUND:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_COUPON);
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_CASH_CARD);
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_GRANT_COUPON);
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_ADD_MEMBER:
                memberType = GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_INFO);
                break;
//            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
//                GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_COUPON);
//                break;
//            case Constant.MEMBER_IDENTIFYING_REFUND:
//                GetMemberIdentifyingList(SktConfigIdCrmConstant.SKT_CONFIG_MEMBER_COUPON);
//                break;
            default:
                break;

        }
        return memberType;
    }

    private static String GetMemberIdentifyingList(String strFlag) {
        String memberType="";
        for(Sktconfig item: ErpCommonData.loadConfig.getSkt().getSktconfigList()){
            if(item.getLx().equals(SktConfigConstant.SKT_CONFIG_TYPE_CRM)){
                if(item.getId().equals(strFlag) ){
                    memberType = item.getValue();
                    break;
                }
            }
        }
        return memberType;
    }

    /**
     *  卡号
     * @param mContext
     * @param flag
     * @param content
     */
    private static void cardNoTypeDialog(Activity mContext, int flag, String content) {
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON:
//                QueryMemberRequest param = new QueryMemberRequest();
//                param.setCondType(1);
//                param.setCondValue(content);
//                param.setCodedCouponType(2);
//                param.setServerBillId(Integer.parseInt(ErpCommonData.ticketInfo.getServerBillId()));
//                param.setSktno(ErpCommonData.sktno);
//                param.setVerifyCode("");
//                param.setPassword("");
//                param.setCardCodeToCheck("");
//                param.setCodedCouponCode("");
//                ((CardCouponPayActivity)mContext).netPresenter.yhqkRequest(param);
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:

                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:

                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                ((RefundActivity)mContext).queryMemberReturn(content,"0");
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_ADD_MEMBER:
                QueryMemberReq memberReq = new QueryMemberReq();
                memberReq.setCondType("0");
                memberReq.setCardNo(content);
                memberReq.setOrderNo(ErpCommonData.ticketInfo.getOrderNo());
                memberReq.setStoreCode(ErpCommonData.storeCode);
                if(mContext instanceof MainActivity) {
                    ((MainActivity) mContext).mSalePresenterImpl.queryMember(memberReq);
                }else if(mContext instanceof CateringMainActivity){
                    ((CateringMainActivity) mContext).queryMember(memberReq);
                }else if(mContext instanceof MenuFoodActivity){
                    ((MenuFoodActivity) mContext).queryMember(memberReq);
                }
                break;
            default:
                break;

        }
    }


    /**
     *  关闭
     * @param mContext
     * @param flag  1卡券  2储值卡  3 编码券  4后返券刷卡   5退货会员识别
     */
    private static void closeTypeDialog(Activity mContext, int flag) {
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_TRADE_ADD_MEMBER://不需处理
                break;
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON://不需处理
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:
                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:
                ((PayActivity)mContext).netPresenter.saveCommitPayRequest(((PayActivity)mContext).mTicket,true);
                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                break;
            default:
                break;

        }
    }

    /**
     * 手机号
     *  @param mContext
     * @param flag 1卡券  2储值卡  3 编码券  4 后返券刷卡   5退货会员识别
     * @param content
     */
    private static void phoneTypeDialog(Activity mContext, int flag, String content) {
        switch (flag){
            case Constant.MEMBER_IDENTIFYING_CARDCOUPON:
                QueryMemberRequest param = new QueryMemberRequest();
                param.setCondType(1);
                param.setCondValue(content);
                param.setCodedCouponType(2);
                param.setServerBillId(Integer.parseInt(ErpCommonData.ticketInfo.getServerBillId()));
                param.setSktno(ErpCommonData.sktno);
                param.setVerifyCode("");
                param.setPassword("");
                param.setCardCodeToCheck("");
                param.setCodedCouponCode("");
//                ((CardCouponPayActivity)mContext).netPresenter.yhqkRequest(param);
                break;
            case Constant.MEMBER_IDENTIFYING_CASHCARD:

                break;
            case Constant.MEMBER_IDENTIFYING_CODECOUPON:
                break;
            case Constant.MEMBER_IDENTIFYING_DELIVERYCOUPON:

                break;
            case Constant.MEMBER_IDENTIFYING_REFUND:
                ((RefundActivity)mContext).queryMemberReturn(content,"1");
                break;
            case Constant.MEMBER_IDENTIFYING_TRADE_ADD_MEMBER://交易添加商品添加会员
                QueryMemberReq memberReq = new QueryMemberReq();
                memberReq.setCondType("1");
                memberReq.setCardNo(content);
                memberReq.setOrderNo(ErpCommonData.ticketInfo.getOrderNo());
                memberReq.setStoreCode(ErpCommonData.storeCode);
                if(mContext instanceof MainActivity) {
                    ((MainActivity)mContext).mSalePresenterImpl.queryMember(memberReq);
                }else if(mContext instanceof CateringMainActivity){
                    ((CateringMainActivity) mContext).queryMember(memberReq);
                }else if(mContext instanceof MenuFoodActivity){
                    ((MenuFoodActivity) mContext).queryMember(memberReq);
                }
                break;
            default:
                break;

        }
    }



}
